<!--
 * @description:
 * @author: cuicui
 * @Date: 2023-11-30 11:47:46
 * @LastEditTime: 2023-11-30 11:58:07
 * @LastEditors: cuicui
-->
<template>
  <a-modal
    title='添加曲线'
    :width='640'
    :visible='visible'
    :confirmLoading='loading'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning='loading'>
      <a-form :form='form' v-bind='formLayout'>
        <a-form-item label='曲线名称'>
          <a-input v-decorator="[
              'line_name',
              { rules: [{ required: true, message: '请输入曲线名称!' }] },
            ]" />
        </a-form-item>
        <a-form-item v-bind='index === 0 ? formItemLayout : formItemLayoutWithOutLabel'
                     :label="index === 0 ? '监控项' : ''"
                     v-for="(k, index) in form.getFieldValue('options')" :key='k.id'>
          <a-row>
            <a-col :span='6'>
              <a-select
                style='width:100%'
                placeholder='请选择系统'
                v-decorator="[
              'device',
              { rules: [{ required: true, message: '请选择设备' }] },
            ]">
                <a-select-option value='0'>系统1</a-select-option>
                <a-select-option value='1'>系统2</a-select-option>
                <a-select-option value='2'>系统3</a-select-option>
              </a-select>
            </a-col>

            <a-col :span='6' :offset='1'>
              <a-select
                style='width:100%'
                placeholder='请选择设备'
                v-decorator="[
              'pooint',
              { rules: [{ required: true, message: '请选择设备' }] },
            ]">
                <a-select-option value='0'>设备1</a-select-option>
                <a-select-option value='1'>设备2</a-select-option>
                <a-select-option value='2'>设备3</a-select-option>
              </a-select>
            </a-col>

            <a-col :span='6' :offset='1'>
              <a-input placeholder='请输入显示名称' />
            </a-col>
            <a-col :span='3' :offset='1'>
              <a-row>
                <a-col :span='12'>
                  <color-picker v-model='k.color' />
                </a-col>
                <a-col :span='12'>
                  <a-icon
                    v-if="form.getFieldValue('options').length > 1"
                    class='dynamic-delete-button'
                    type='minus-circle-o'
                    style='color: red'
                    :disabled="form.getFieldValue('options').length === 1"
                    @click='() => remove(k)'
                  />
                </a-col>
              </a-row>
            </a-col>
          </a-row>
        </a-form-item>
        <a-row>
          <a-button type='dashed' style='width: 100%' @click='add'>
            <a-icon type='plus' />
            增加监控项
          </a-button>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import ColorPicker from '@/components/ColorPicker/index.vue'
import pick from 'lodash.pick'

// 表单字段
const fields = ['sys-type', 'equipment_type', 'point_type']

let id = 0
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  components: {
    ColorPicker
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      }
    }
    return {
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 21 }
        }
      },
      formItemLayoutWithOutLabel: {
        wrapperCol: {
          xs: { span: 24, offset: 0 },
          sm: { span: 21, offset: 3 }
        }
      }
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    this.form.getFieldDecorator('options', { initialValue: [{ id }], preserve: true })
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    remove(value) {
      const { form } = this
      const { id: currentId } = value
      // can use data-binding to get
      const options = form.getFieldValue('options')
      // We need at least one passenger
      if (options.length === 1) {
        return
      }

      // can use data-binding to set
      form.setFieldsValue({
        options: options.filter(item => item.id !== currentId)
      })
    },

    add() {
      const { form } = this
      // can use data-binding to get
      const options = form.getFieldValue('options')
      id++
      // important! notify form to detect changes
      form.setFieldsValue({
        options: [...options, { id }]
      })
    }
  }
}
</script>
